require "base_def"

local hand_channel = {}

--解析渠道信息
local function parse_channel_info(info)
    local res = {}
    for field, value in pairs(info or {}) do
        if field == 'open_game_id' then
            local ok, list = pcall(cjson.decode, value)
            if ok then
                res.open_game_id = list
            end
        --对游客开发的游戏
        elseif field == 'guest_open_game' then
            local ok, list = pcall(cjson.decode, value)
            if ok then
                res.guest_open_game = list
            end
        elseif field == 'transfer_min_max' then
            local ok, min_max = pcall(cjson.decode, value)
            if ok then
                res.transfer_min_gold = min_max[1]
                res.transfer_max_gold = min_max[2]
            end
        elseif field == 'transfer_limit' then
            local ok, transfer_limit = pcall(cjson.decode, value)
            if ok then
                res.transfer_frozen_gold = transfer_limit[1]
                res.transfer_rate = transfer_limit[2]
                --关闭转账
                res.close_transform = (transfer_limit[3] ~= 1)      
                res.cross_package_trans = (transfer_limit[4] == 1)
            end
        elseif field == 'withdraw_min_max' then
            local ok, withdraw_min_max = pcall(cjson.decode, value)
            if ok then
                res.cash_min_gold = withdraw_min_max[1]
                res.cash_max_gold = withdraw_min_max[2]
            end
        elseif field == 'withdraw_limit' then
            local ok, withdraw_limit = pcall(cjson.decode, value)
            if ok then
                res.cash_frozen_gold = withdraw_limit[1]
                res.withdraw_rate = withdraw_limit[2]
                --关闭取款
                res.close_withdraw = (withdraw_limit[3] ~= 1)        
            end
        elseif field == 'rebate_rate' then
            local ok, rebate_rate = pcall(cjson.decode, value)
            if ok then
                res.firstRebate  = rebate_rate[1]
                res.secondRebate = rebate_rate[2]
                res.thirdRebate  = rebate_rate[3]
            end
        else
            res[field] = value
        end
    end
    return res
end

local function get_channel_Info(channel)
    local str = string.format("call pr_get_channel_cfg('%s')", channel)
    local ret = CallMysql(str)
    --log.info("get_channel_Info ret=", ret)
    if ret and ret[1] then
        local raw_info = ret[1][1]
        if raw_info then
            local info = parse_channel_info(raw_info)
            return info
        end
    else
        log.error("get_channel_Info error str= ", str)
    end
end

--根据传参获取部分渠道信息
function hand_channel.GetChannelInfo(channel, req)
    log.info('hand_channel.GetChannelInfo req= ', req)
    local res = {}
    local ret = get_channel_Info(channel)
    --log.info("hand_channel.GetChannelInfo=", ret)
    if ret then 
        for _, field in pairs(req or {}) do
            res[field] = ret[field]
        end
        return true, res
    end

    return 
end

--获取完整渠道信息
function hand_channel.GetChannelInfoAll(channel)
    local info = get_channel_Info(channel)
    if info then 
        --log.info("info=", info)
        return true, info
    end

    log.error("hand_channel.GetChannelInfoAll failure!")
    return 
end

return hand_channel